package Algo;

import java.io.File;


public class Tree 
{
	
	
	private static Node make_tree (File dir)
	{
		Node root = new Node (dir); 
		if (dir.isDirectory())
		{
			make_tree_iter (root);
			return root;
		}
		else 
		{
			return root;
		}
	}
	
	
	private static void make_tree_iter (Node cur_node)
	{
		File list [] = cur_node.file.listFiles();
		if (list.length<1)
		{
			if (list.length==0)
				return;
			cur_node.son=new Node (list[0]);
			if (list[0].isDirectory()) 
				make_tree_iter (cur_node.son);
		}
		else 
		{ 
			cur_node.son = new Node (list[0]);
			cur_node=cur_node.son;
			if (list[0].isDirectory()) 
				make_tree_iter (cur_node.son);
			for (int i=1; i<list.length; i++)
			{
				cur_node.brother = new Node (list[i]);
				cur_node=cur_node.brother;
				if (list[i].isDirectory()) 
					make_tree_iter (cur_node);
				
			}
		}
	}
	static int level=0;
	public static void print_tree (Node node)
	{
		if (node==null)
			return;
		for (int i=0; i<level; i++)
			System.out.print ("\t");
		System.out.print (node.file.getName());
		
		if (node.son!=null)
		{
			level ++;
			System.out.print ("\n");
			for (int i=0; i<level; i++)
				System.out.print ("\t");
			print_tree (node.son);
		}
		if (node.brother!=null)
		{
			System.out.print ("\n");
			for (int i=0; i<level; i++)
				System.out.print ("\t");
			print_tree (node.brother);
		}
		else
		{	level=level-1;}		
		
		
		
		
		
	}
	
	public static void main(String[] args) 
	{
		File dir = new File ("d:/1");
		Node root = make_tree (dir);
		print_tree(root);
		
	}
	

}